package com.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.mapper.ChanageMapper;
import com.sys.mapper.EmployeeMapper;
import com.sys.pojo.Chanage;
import com.sys.pojo.Employee;
import com.sys.service.ChanageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZJF
 * @since 2021-04-01
 */
@Service
public class ChanageServiceImpl extends ServiceImpl<ChanageMapper, Chanage> implements ChanageService {

    @Resource
    private ChanageMapper chanageMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public IPage<Chanage> findListByPage(Chanage chanage) {
        IPage<Chanage> iPage = new Page<>(chanage.getPage(), chanage.getLimit());
        LambdaQueryWrapper<Chanage> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(chanage.getEname())) {
            LambdaQueryWrapper<Employee> employeeQueryWrapper = new LambdaQueryWrapper<>();
            employeeQueryWrapper.like(Employee::getEmployeeName, chanage.getEname());
            List<Employee> employees = employeeMapper.selectList(employeeQueryWrapper);
            for (Employee e : employees) {
                queryWrapper.eq(Chanage::getEid, e.getId()).or();
            }
        }
        IPage<Chanage> chanageIPage = chanageMapper.selectPage(iPage, queryWrapper);
        for (Chanage t : chanageIPage.getRecords()) {
            Employee employee = employeeMapper.selectById(t.getEid());
            t.setEname(employee.getEmployeeName());
        }

        return chanageIPage;
    }

}
